{
  class Node {
    constructor(
      public element: any = null,
      public previous: Node | null = null,
      public next: Node | null = null
    ) {}
  }

  class LinkList<T> {
    public head = new Node("head");

    private find(item: T): Node {
      let current = this.head;
      while (current.next) {
        if (current.element === item) {
          break;
        } else {
          current = current.next;
        }
      }
      return current;
    }

    private findPrevious(item: T): Node {
      let current = this.head;
      while (current.next) {
        if (current.next.element === item) {
          break;
        } else {
          current = current.next;
        }
      }
      return current;
    }

    private findLast(): Node {
      let curent = this.head;
      while (curent.next) {
        curent = curent.next;
      }
      return curent;
    }

    public insert(element: T, item: T) {
      let current = this.find(item);
      let newNode = new Node(element);
      if (current.next) {
        // 双向链表插入元素
        current.next.previous = newNode;
        newNode.next = current.next;
        newNode.previous = current;
        current.next = newNode;
      } else {
        current.next = newNode;
        newNode.previous = current;
      }
    }

    public remove(item: T) {
      let currNode = this.find(item);
      if (!(currNode.next == null) && !(currNode.previous == null)) {
        currNode.previous.next = currNode.next;
        currNode.next.previous = currNode.previous;
        currNode.next = null;
        currNode.previous = null;
      }
    }

    public display() {
      let currNode = this.head;
      // console.log(currNode);
      while (currNode) {
        console.log(currNode.element, currNode.previous?.element);
        if (currNode.next) {
          currNode = currNode.next;
        } else {
          break;
        }
      }
    }

    public dispReverse(): void {
      let last = this.findLast();
      // console.log(last.element);
      while (last) {
        console.log(last.element);
        if (last.previous) {
          last = last.previous;
        } else {
          break;
        }
      }
    }
  }

  let doubleLink = new LinkList<string>();

  doubleLink.insert("jimmy", "head");
  doubleLink.insert("xuexue", "jimmy");
  doubleLink.insert("henry", "xuexue");
  doubleLink.insert("jack", "henry");
  // doubleLink.remove("xuexue");
  doubleLink.display();
  console.log("*************************");
  doubleLink.dispReverse();
}
